/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var xml, xsl;
var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var xslproc;

load = function(){
    var xmlurl = "XMLGetController?action=xml&fileName=albums";
    xml = loadXML(xmlurl);

    var xslurl = "XMLGetController?action=xsl&fileName=viewAlbums";
    xsl = loadXML(xslurl);

    if (window.ActiveXObject)
    {
        // Load XML

        
        xsldoc.async = false;
        xsldoc.load("XMLGetController?action=xsl&fileName=viewAlbums");
        if (xsldoc.parseError.errorCode != 0) {
            var myErr = xsldoc.parseError;
            WScript.Echo("You have error " + myErr.reason);
        } else {
            xslt.stylesheet = xsldoc;
            xmldoc.async = false;
            xmldoc.load("XMLGetController?action=xml&fileName=albums");
            if (xmldoc.parseError.errorCode != 0) {
                var myErr = xmldoc.parseError;
                WScript.Echo("You have error " + myErr.reason);
            } else {

        }
        }
    }
}

function loadXML(dname)
{
    if (window.XMLHttpRequest)
      {
          xhttp=new XMLHttpRequest();
      }
    else
      {
          xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send(null);
    return xhttp.responseXML;
}

function displayResult() {
    var searchText = document.getElementById("searchBox").value;       
    xslproc = xslt.createProcessor();
    xslproc.input = xmldoc;
    xslproc.addParameter("searchText",searchText.toLowerCase());
    xslproc.transform();
    document.getElementById("main-left-inner").innerHTML=xslproc.output;
}

function createNewAlbum()
{
    var name=prompt("Please enter Album Name","");
    if (name!=null && name!="")
    {
        var url = "Controller?txtAlbumName="+encodeURIComponent(name)+"&action=Create+Album";
        window.location.href = url;
    }
}

//function ajaxRequest(requestType,url) {
//
//    var xmlhttp;
//
//    if (window.XMLHttpRequest) {
//        xmlhttp = new XMLHttpRequest();
//    } else {
//        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//    }
//
//    xmlhttp.onreadystatechange = function()
//    {
//
//        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
//        {
//            switch (requestType)
//            {
//                case 'album':
//                    //Home postcode search
//                    alert(xmlhttp.responseText);
//                    var album = JSON.parse(xmlhttp.responseText);
//                    var newAlbum = '<div class="left"><a href="viewImage.jsp?albumId='+ album.id +'">\n\
                    
//                    alert(newAlbum);
//                    break;
//            }
//        }
//    }
//    xmlhttp.open("GET", url, true);
//    xmlhttp.send();
//}